import axios from "axios";
axios.defaults.withCredentials = true;

export default function(url,getOrPost,data,func){
    if(getOrPost === 'get'){
        if(data != null){
            axios.get(url,data).then(
                response => {
                    if(response.data.result === 'success'){
                        func(response.data.data);
                    }else{
                        console.log(response.data.msg);
                        return;
                    }
                },
                error => {
                    if(error.response.status === 701){
                        window.location.replace('http://localhost:8080/');
                    }else{
                        console.log(error.message);
                    }
                }
            )
        }else{
            axios.get(url).then(
                response => {
                    if(response.data.result === 'success'){
                        func(response.data.data);
                    }else{
                        console.log(response.data.msg);
                        return;
                    }
                },
                error => {
                    if(error.response.status === 701){
                        window.location.replace('http://localhost:8080/');
                    }else{
                        console.log(error.message);
                    }
                }
            )
        }
    }else{
        if(data != null){
            axios.post(url,data).then(
                response => {
                    if(response.data.result === 'success'){
                        func(response.data.data);
                    }else{
                        console.log(response.data.msg);
                        return;
                    }
                },
                error => {
                    if(error.response.status === 701){
                        window.location.replace('http://localhost:8080/');
                    }else{
                        console.log(error.message);
                    }
                }
            )
        }else{
            axios.post(url).then(
                response => {
                    if(response.data.result === 'success'){
                        func(response.data.data);
                    }else{
                        console.log(response.data.msg);
                        return;
                    }
                },
                error => {
                    if(error.response.status === 701){
                        window.location.replace('http://localhost:8080/');
                    }else{
                        console.log(error.message);
                    }
                }
            )
        }
    }
}